<?php
/*	
	结算
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'结算列表'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('balance',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array(
			'tip'=>'[温馨提示]：考虑PC后台创建结算的麻烦性，所有结算订单的创建，直接由代理商客户手机端自由申请结算！',
			'data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'分销商','type'=>'member'),
			array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
			array('name'=>'commission_num','title'=>'佣金份数','type'=>'string'),
			array('name'=>'money','title'=>'佣金金额','type'=>'string'),
			array('name'=>'jifen','title'=>'积分数','type'=>'string'),
			array('name'=>'balance_pay','title'=>'手续费','type'=>'string'),
			array('name'=>'final_money','title'=>'打款金额','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'check_time','title'=>'审核时间','type'=>'time'),
			array('name'=>'pay_time','title'=>'打款时间','type'=>'time'),
			array('name'=>'status','title'=>'结算单状态','type'=>'radio','options'=>'balance_status'),
			array('name'=>'pay_type','title'=>'打款方式','type'=>'radio','options'=>'pay_types'),
		),'action'=>array(
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
			array('type'=>'modal','op'=>'pay','title'=>'打款','show'=>array('status'=>1)),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>18,'action'=>'jy_merch/manage.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'balance_status'),
		),
	);
	$return['htmls']=$htmls;
	$return['balance_status']=$this->balance_status;
	$return['pay_types']=$this->pay_types;
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$balance_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('commission')->check_balance($balance_id,$status,$post['remark']);
		exi('审核成功！','','list');
	}
	$item=$this->get('balance',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核结算单',
		'edit'=>array(
			array('type'=>'string','name'=>'balancesn','title'=>'结算单号','read'=>true),
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'balance_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['balance_status']=array(
		'1'=>'审核通过',
		'2'=>'审核不通过',
	);
	exi($return);
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$balance_id=intval($post['id']);
		$pay_type=trim($post['pay_type']);
		if(!$pay_type){
			exi('请选择打款方式！','error');
		}
		$this->model('commission')->pay_balance($balance_id,$pay_type,$post['wxgid'],$post['pay_trade_no']);
		exi('打款成功！','','list');
	}
	$item=$this->get('balance',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	$item['pay_typetitle']=$item['pay_type']?$this->pay_types[$item['pay_type']]:'';
	$item['noselect']=$item['pay_type']?0:1;
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'打款',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，必须选择打款《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('type'=>'string','name'=>'balancesn','title'=>'结算单号','read'=>true),
			array('type'=>'string','name'=>'money','title'=>'结算金额','read'=>true),
			array('type'=>'string','name'=>'final_money','title'=>'最终金额','read'=>true),
			array('name'=>'pay_typetitle','title'=>'打款方式','type'=>'string','read'=>true,'show'=>array('noselect'=>0)),			
			array('type'=>'copy','name'=>'realname','title'=>'真实姓名','show'=>array('pay_type'=>array('alipay','union','jiaoyi'),'noselect'=>0)),
			array('type'=>'copy','name'=>'alipay','title'=>'支付宝帐号','show'=>array('pay_type'=>array('alipay'),'noselect'=>0)),
			array('type'=>'copy','name'=>'bankname','title'=>'所属银行','show'=>array('pay_type'=>array('union'),'noselect'=>0)),
			array('type'=>'copy','name'=>'bankcard','title'=>'银行卡号','show'=>array('pay_type'=>array('union'),'noselect'=>0)),
			array('type'=>'copy','name'=>'bankcard','title'=>'交易所账户','show'=>array('pay_type'=>array('jiaoyi'),'noselect'=>0)),
			array('type'=>'string','name'=>'pay_trade_no','title'=>'打款编号','show'=>array('pay_type'=>array('alipay','union'))),
			array('name'=>'pay_type','title'=>'打款方式','type'=>'radio','options'=>'pay_types','show'=>array('noselect'=>1)),
		),
	);
	if($this->setting['withdrawtype']){
		$withdrawtype=array();
		$check=strstr($this->setting['withdrawtype'],'credit');
		if($check){
			$withdrawtype['credit']=$this->pay_types['credit'];
		}
		$check=strstr($this->setting['withdrawtype'],'wechat');
		if($check){
			$withdrawtype['wechat']=$this->pay_types['wechat'];
		}
		$check=strstr($this->setting['withdrawtype'],'alipay');
		if($check){
			$withdrawtype['alipay']=$this->pay_types['alipay'];
		}
		$check=strstr($this->setting['withdrawtype'],'union');
		if($check){
			$withdrawtype['union']=$this->pay_types['union'];
		}
		$check=strstr($this->setting['withdrawtype'],'credit');
		if($check){
			$withdrawtype['credit']=$this->pay_types['credit'];
		}
		$return['pay_types']=$withdrawtype;
	}else{
		$return['pay_types']=$this->pay_types;
	}
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('balance',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('balance',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}